package c.w.utils;
/**
 * @author 刘红军 
 * 加入栈：ExitManager.getScreenManager().addStack(this);
 * 关闭程序： ExitManager.getScreenManager().popAllStack();
 * */
import java.util.Stack;

import android.app.Activity;
import android.util.Log;

public class ExitManager {

	private static ExitManager instance;
	private static Stack<Activity> stackActivity  = new Stack<Activity>(); 
	
	public ExitManager(){
		
	}
	public ExitManager getScreenManager(){
		
		if(instance == null){
			
			instance = new ExitManager();
		}
		
		return instance;
		
	}
	
	/**
	 * 回收指定的栈
	 * 
	 * */
	public void popActivity(Activity activity){
		
		if(activity != null){
			activity.finish();
			stackActivity.remove(activity);
			activity = null;
		}
	}
	//获得当前栈顶Activity
	public Activity topActivity(){
		Activity activity = null;
		try{
		if(!stackActivity.isEmpty()){
			activity = stackActivity.pop();
				
		}
		return activity;
		}catch (Exception e) {
			// TODO: handle exception
			Log.i("info", e.getMessage());
			return activity;
		}finally{
			activity = null;
		}
		
	}
	//压入堆栈中
	
	public void addStack(Activity activity){
		
		if(stackActivity == null){
			stackActivity  = new Stack<Activity>();
		}
		stackActivity.push(activity);
	}
	//回收所有栈
	public void popAllStack(){
		Activity activity = null;
		try{
		while(!stackActivity.isEmpty()){
			activity = topActivity();
			if(activity != null){
				popActivity(activity);
			}
		}
		}finally{
			activity = null;
		}
	}
}
